package com.icode.dao.impl;

import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.manage.Contacts;
import com.icode.core.model.manage.MessageModel;
import com.icode.core.model.manage.ModelTemplate;
import com.icode.dao.MessageDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-7-14
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
@Repository("messageDao")
public class MessageDaoImpl extends EntityDaoImpl implements MessageDao {


    @Override
    public List<Contacts> findContacts(String currentPage) {
        String hql = "from " + Contacts.class.getName();
        Query query = currentSession().createQuery(hql);
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageOfData);
        query.setMaxResults(PageDTO.pageOfData);
        return query.list();
    }

    @Override
    public void delContacts(Contacts contacts) {
        currentSession().delete(contacts);
    }

    @Override
    public void delmoreContacts(String[] contactsGuids) {
        String hql = "delete from " + Contacts.class.getName() + " a  where a.guid in (:guids)";
        Query query = currentSession().createQuery(hql);
        query.setParameterList("guids", contactsGuids);
        query.executeUpdate();
    }

    @Override
    public List<MessageModel> findMessageModelByName(ModelTemplate template) {
        String hql = "from " + MessageModel.class.getName() + " a where a.name =:name";
        Query query = currentSession().createQuery(hql);
        query.setParameter("name", template.toString());
        return query.list();
    }

    @Override
    public List<MessageModel> findAllMessageModel() {
        String hql = "from " + MessageModel.class.getName();
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public void deleteObject(Object o) {
        currentSession().delete(o);
    }
}
